<script>
import pagination from '@crud/PaginationCustom.vue'
import CRUD, { presenter, header, form, crud } from '@/components/Crud/customCrud'
const defaultForm = {
  id: null,
  name: '', // 客户端名称
  key: '', // 密钥
  ipAddress: '', // IP白名单
  ls: 0, // 流速
  loginUrl: '', // 系统登录地址
  sfxl: false, // 是否限流
  state: true // 是否启用
}
export default {
  name: 'ClientHostManager',
  components: { pagination },
  cruds() {
    return CRUD({
      title: '客户端管理',
      url: 'rest/api/sys/notice/v1/client',
      props: {
        searchToggle: true
      }
    })
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  methods: {
    copyValue(value) {
      const oInput = document.createElement('input')
      oInput.value = value
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      this.$message.success('复制成功')
      oInput.remove()
    }
  }
}
</script>

<template>
  <div class="app-container">
    <el-card class="box-card">
      <div
        v-for="(item,index) in crud.data"
        :key="'data-'+index"
        class="box-ul-li"
      >
        <div class="box-ul-header">
          {{ item.name }}
          <div v-if="item.state" class="header-state start">
            启用
          </div>
          <div v-else class="header-state disabled">
            禁用
          </div>
        </div>
        <div class="box-ul-line" @click.stop="copyValue(item.key)">
          key:<br>{{ item.key }}
        </div>
        <div class="box-ul-footer">
          <div class="box-ul-btn" @click="crud.toEdit(item)">编辑</div>
          <div class="box-ul-btn" @click="crud.doDelete(item)">删除</div>
        </div>
      </div>
      <div class="box-ul-li add">
        <div class="box-ul-content" @click="crud.toAdd">
          <div class="box-ul-icon">
            <i class="el-icon-plus" />
          </div>
          <div class="box-ul-title">
            添加客户端
          </div>
        </div>
      </div>
      <!--分页组件-->
      <div class="box-pager">
        <pagination />
      </div>
    </el-card>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        size="small"
        label-width="120px"
      >
        <el-form-item
          :rules="[{ required: true, message: '请输入客户名称', trigger: 'blur' }]"
          label="客户名称"
          prop="name"
        >
          <el-input v-model="form.name" placeholder="请输入客户名称" style="width: 400px;" />
        </el-form-item>
        <el-form-item
          label="客户端登录地址"
          prop="loginUrl"
        >
          <el-input v-model="form.loginUrl" placeholder="请输入客户端登录地址" style="width: 400px;" />
        </el-form-item>
        <el-form-item
          label="是否限流"
          prop="sfxl"
          :rules="[{ required: true, message: '请选择是否限流', trigger: 'changer' }]"
        >
          <el-radio-group
            v-model="form.sfxl"
          >
            <el-radio
              v-for="item in enable"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="form.sfxl === true"
          label="流速"
          prop="ls"
          :rules="[{ required: true, message: '请输入流速', trigger: 'blur' }]"
        >
          <el-input v-model="form.ls" placeholder="请输入流速" style="width: 400px;" />
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: '请输入客户端IP白名单', trigger: 'blur' }]"
          label="IP白名单"
          prop="ipAddress"
        >
          <el-input v-model="form.ipAddress" type="textarea" placeholder="请输入客户端IP白名单" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group
            v-model="form.state"
          >
            <el-radio
              v-for="item in enable"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
    <notice-client-auth-setting ref="set" />
  </div>
</template>
<style scoped>
.el-card ::v-deep .el-card__body{
  width: 100%;
  display: flex;
  flex-flow: row wrap;
}
.box-card {
  min-height: 100vh;
  width: 100%;
}
.box-ul-li {
  width: 275px;
  height: 110px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  color: #99a9bf;
  cursor: pointer;
  margin-left: 10px;
  margin-top: 10px;
  padding: 0 10px;
  box-sizing: border-box;
}
.box-ul-li.add {
  border: 1px dashed #DCDFE6;
  text-align: center;
}
.box-ul-li.add:hover {
  border: 1px dashed #5cb6ff;
  color: #5cb6ff ;
}
.box-ul-content {
  width: 100%;
  height: 60px;
  margin-top: 20px;
}
.box-ul-icon {
  width: 100%;
  height: 40px;
  line-height: 40px;
  font-size: 32px;
}
.box-ul-title {
  width: 100%;
  height: 20px;
  line-height: 20px;
  font-size: 14px;
}
.box-ul-header {
  height: 40px;
  line-height: 40px;
  text-align: left;
  font-size: 14px;
  color: #2c3e50;
  font-weight: bold;
  position: relative;
}
.box-ul-line {
  width: 100%;
  height: 40px;
  line-height: 20px;
  text-align: left;
  color: #909399;
  font-size: 14px;
}
.box-ul-footer {
  width: 100%;
  height: 30px;
  line-height: 30px;
  display: flex;
}
.box-ul-btn {
  width: 40px;
  height: 30px;
  position: relative;
  text-align: center;
  line-height: 30px;
  font-size: 13px;
  color: #409eff;
}
.box-ul-btn:not(:first-child):before {
  content: '';
  position: absolute;
  width: 2px;
  height: 14px;
  background-color: #909399;
  top: 8px;
  left: -1px;
}
.header-state {
  position: absolute;
  width: 60px;
  height: 30px;
  font-size: 14px;
  top: 5px;
  text-align: center;
  line-height: 30px;
  right: -10px;
}
.header-state.start {
  color: #67C23A;
}
.header-state.disabled {
  color: #F56C6C;
}
.start:after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  top: 10px;
  right: 48px;
  background-color: #67C23A;
}
.disabled:after {
  content: '';
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 5px;
  top: 10px;
  right: 48px;
  background-color: #F56C6C;
}
.box-pager {
  width: 100%;
  height: 50px;
  margin-top: 20px;
}
</style>
